package com.fdhwws.web.service.impl;

import com.fdhwws.web.dao.ChatRoomDao;
import com.fdhwws.web.dao.UserDao;
import com.fdhwws.web.dao.impl.ChatRoomDaoImpl;
import com.fdhwws.web.dao.impl.UserDaoImpl;
import com.fdhwws.web.domain.ChatRoom;
import com.fdhwws.web.domain.User;
import com.fdhwws.web.entity.AjaxResult;
import com.fdhwws.web.service.ChatRoomService;
import com.fdhwws.web.service.UserService;
import com.fdhwws.web.util.InfoSecurityUtil;

import java.util.ArrayList;
import java.util.List;

public class ChatRoomServiceImpl implements ChatRoomService {
    private final ChatRoomDao chatRoomDao = new ChatRoomDaoImpl();
    private final UserDao userDao = new UserDaoImpl();

    @Override
    public AjaxResult addChatRoom(ChatRoom chatRoom, User user) {
        if (chatRoom.getAccess() == 0 && (chatRoom.getPassword() == null || chatRoom.getPassword().length() < 4 || chatRoom.getPassword().length() > 20)) {
            return AjaxResult.error("密码长度必须在4-20个字符之间!", null);
        }
        chatRoom.passwordEncrypt();
        int insertId = chatRoomDao.insert(chatRoom);
        if (insertId <= 0) {
            return AjaxResult.error("添加聊天室失败！", null);
        }
        userDao.update(user);
        return AjaxResult.success("添加聊天室成功！", null);
    }

    @Override
    public Boolean checkChatRoom(String name) {
        ChatRoom room = chatRoomDao.selectChartRoomByName(name);
        return room == null;
    }

    @Override
    public List<ChatRoom> getChatRoomByUid(Integer uid) {
        if (uid == null) {
            return new ArrayList<>();
        }
        return chatRoomDao.selectChatRoomsByUid(uid);
    }

    @Override
    public List<ChatRoom> getAllChatRoom() {
        return chatRoomDao.selectAll();
    }

    @Override
    public Boolean verifyChatRoom(Integer id, String pwd) {
        ChatRoom chatRoom = chatRoomDao.selectById(id);
        if (chatRoom == null || !chatRoom.getValid()) {
            return false;
        } else {
            if (chatRoom.getAccess() == 0) {
                if (pwd != null && chatRoom.getPassword().equals(InfoSecurityUtil.encrypt(pwd))) {
                    return true;
                }
                return false;
            }
            return true;
        }
    }

    @Override
    public AjaxResult getChatRoomByRoomId(Integer id) {
        ChatRoom chatRoom = chatRoomDao.selectById(id);
        if (chatRoom == null) {
            return AjaxResult.error("聊天室不存在！", null);
        } else {
            return AjaxResult.success("获取聊天室成功！", chatRoom);
        }
    }

    @Override
    public AjaxResult deleteChatRoomById(Integer id, User user) {
        ChatRoom chatRoom = new ChatRoom();
        chatRoom.setId(id);
        chatRoom.setValid(false);
        Integer i = chatRoomDao.updateByRoomIdAndUid(chatRoom, user.getId());
        if (i > 0) {
            return AjaxResult.success("删除聊天室成功！", null);
        } else {
            return AjaxResult.error("删除聊天室失败！", null);
        }
    }

    @Override
    public AjaxResult updateChatRoom(ChatRoom chatRoom) {
        if (chatRoom.getId() == null) {
            return AjaxResult.error("聊天室不存在！", null);
        }
        if (chatRoom.getAccess() == 0 && (chatRoom.getPassword() == null || chatRoom.getPassword().length() < 4 || chatRoom.getPassword().length() > 20)) {
            return AjaxResult.error("密码长度必须在4-20个字符之间!", null);
        }
        if (chatRoom.getAccess() == 0) {
            chatRoom.setPassword(InfoSecurityUtil.encrypt(chatRoom.getPassword()));
        } else {
            chatRoom.setPassword("");
        }
        int update = chatRoomDao.update(chatRoom);
        if (update > 0) {
            return AjaxResult.success("修改聊天室成功！", null);
        } else {
            return AjaxResult.error("修改聊天室失败！", null);
        }
    }

    @Override
    public AjaxResult openOrCloseChatRoomByRoomId(Integer roomId) {
        ChatRoom chatRoom = chatRoomDao.selectById(roomId);
        if (chatRoom == null) {
            return AjaxResult.error("聊天室不存在！", null);
        } else {
            ChatRoom newRoom = new ChatRoom();
            newRoom.setId(roomId);
            newRoom.setValid(!chatRoom.getValid());
            int update = chatRoomDao.update(newRoom);
            if (update > 0) {
                return AjaxResult.success("修改聊天室状态成功！", null);
            } else {
                return AjaxResult.error("修改聊天室状态失败！", null);
            }
        }
    }
}
